<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Manual_event_tournament_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	function add_new_category_submit(){
		$category = $this->input->post('category', TRUE);
		$tournament_id = $this->input->post('tournament_id', TRUE);
		$category_type = $this->input->post('category_type', TRUE);
		$number_boxes = $this->input->post('number_boxes', TRUE);
		$range_denote = $this->input->post('range_denote', TRUE);
		$odds = $this->input->post('odds', TRUE);
		
		if($this->phpvalidator->is_empty($category)){
			echo "Category is required";exit();
		}
		
		if(! $this->phpvalidator->is_numeric($odds) OR $odds <= 0){
			echo "Odds is not valid.";
			exit();
		}
		
		if($category_type == 'box'){
			if( ! $this->phpvalidator->is_natural($number_boxes) OR $number_boxes <= 0){
				echo "Number boxes is not valid";exit();
			}
		}
		if($category_type == 'range'){
			if( ! $this->phpvalidator->is_natural($range_denote) OR $range_denote <= 0){
				echo "Range is not valid";exit();
			}
		}
		
		$CATEGORY = $this->category_manual_tournament_entity->createNewEntity();
		
		$CATEGORY->setTournamentId($tournament_id);
		$CATEGORY->setCategoryName($category);
		$CATEGORY->setStatus('living');
		$CATEGORY->setType($category_type);
		$CATEGORY->setIsOpen('yes');
		$CATEGORY->setOdds($odds);
		 
		if($category_type == 'box'){
			$CATEGORY->setNumberBoxes($number_boxes);
		}else{
			$CATEGORY->setRangeDenote($range_denote);
		}
		$CATEGORY->setOrdering($CATEGORY->getNextOrdering($tournament_id));
		$CATEGORY->update();
		echo 'ok';exit();
	}
	
	function edit_category_submit(){
		$category = $this->input->post('category', TRUE);
		$tournament_id = $this->input->post('tournament_id', TRUE);
		$category_id = $this->input->post('cat_id', TRUE);
		$number_boxes = $this->input->post('number_boxes', TRUE);
		$range_denote = $this->input->post('range_denote', TRUE);
		$odds = $this->input->post('odds', TRUE);
		$ordering = abs( intval( $this->input->post('ordering', TRUE) ) );
		
		$CATEGORY = $this->category_manual_tournament_entity->init('id',$category_id);
		
		if($this->phpvalidator->is_empty($category)){
			echo "Category is required";exit();
		}
		if(! $this->phpvalidator->is_numeric($odds) OR $odds <= 0){
			echo "Odds is not valid.";
			exit();
		}
		if($CATEGORY->getType() == 'box'){
			if( ! $this->phpvalidator->is_natural($number_boxes) OR $number_boxes <= 0){
				echo "Number boxes is not valid";exit();
			}
		}
		if($CATEGORY->getType() == 'range'){
			if( ! $this->phpvalidator->is_natural($range_denote) OR $range_denote <= 0){
				echo "Range is not valid";exit();
			}
		}
		
		$CATEGORY->setCategoryName($category);
		$CATEGORY->setOdds($odds);
		$CATEGORY->setOrdering($ordering);
		if($CATEGORY->getType() == 'box'){
			$CATEGORY->setNumberBoxes($number_boxes);
		}else{
			$CATEGORY->setRangeDenote($range_denote);
		}
		$CATEGORY->update();
		echo 'ok';exit();
	}
	
	function manual_category_set_answer_submit(){
		$cat_id = $this->input->post('cat_id', TRUE);
		$answer = $this->input->post('answer', TRUE);
		$fix_answer = $this->input->post('fix_answer', TRUE);
		$CAT = $this->category_manual_tournament_entity->init('id',$cat_id);
		
		if(strlen($answer) <1){
			echo 'Answer must not empty.';exit;
		}
		
		$CAT->setFixAnswer($fix_answer);
		$CAT->setCorrectAnswer($answer);
		$CAT->update();
		echo 'ok';exit();
	}
	
	function change_manual_category_open_hide(){
		$category_id = $this->input->post('cat_id', TRUE);
		$CATEGORY = $this->category_manual_tournament_entity->init('id',$category_id);
		$new_status = ($CATEGORY->getIsOpen() == 'yes') ? 'no':'yes';
		
		$CATEGORY->setIsOpen($new_status);
		$CATEGORY->update();
		echo $CATEGORY->getTournamentId();exit();
	}
	
	function set_bet_manual_event_result(){
		$bet_id = $this->input->post('bet_id', TRUE);
		$result = $this->input->post('result', TRUE);
		$BET_MANUAL = $this->bet_manual_tournament_entity->init('id',$bet_id);
		$BET_MANUAL->setResult($result);
		$BET_MANUAL->update();
		$CAT = $this->category_manual_tournament_entity->init('key_id',$BET_MANUAL->getCategoryId());
		echo $CAT->getId();exit();
	}
	
	function bet_manual_event_finish(){
		$bet_id = $this->input->post('bet_id', TRUE);
		$BET_MANUAL = $this->bet_manual_tournament_entity->init('id',$bet_id);
		if(!$BET_MANUAL->getResult()){
			echo "Please set result before.";exit();
		}
		$BET_MANUAL->setStatus('finished');
		$BET_MANUAL->processUserBetResult();
		$BET_MANUAL->update();
		
		$CAT = $this->category_manual_tournament_entity->init('key_id',$BET_MANUAL->getCategoryId());
		echo $CAT->getId();exit();
	}
	
	function bet_manual_event_cancel(){
		$bet_id = $this->input->post('bet_id', TRUE);
		$BET_MANUAL = $this->bet_manual_tournament_entity->init('id',$bet_id);
		$BET_MANUAL->setStatus('cancelled');
		$BET_MANUAL->setResult('cancelled');
		$BET_MANUAL->processUserBetResult();
		$BET_MANUAL->update();
		
		$CAT = $this->category_manual_tournament_entity->init('key_id',$BET_MANUAL->getCategoryId());
		echo $CAT->getId();exit();
	}
	
	function manual_category_cancel(){
		$CAT_MANUAL = $this->category_manual_tournament_entity->init('id', $_POST['cat_id']);
		$bettingList = $this->bet_manual_tournament_entity->getBettingList($CAT_MANUAL->getKeyId());
		foreach($bettingList as $item){
			$BET_MANUAL = $this->bet_manual_tournament_entity->init('id',$item->id);
			$BET_MANUAL->setStatus('cancelled');
			$BET_MANUAL->setResult('cancelled');
			$BET_MANUAL->processUserBetResult();
			$BET_MANUAL->update();
		}
		$CAT_MANUAL->setStatus('cancelled');
		$CAT_MANUAL->update();
		
		echo $CAT_MANUAL->getTournamentId();exit();
	}
	
	function manual_category_finish(){
		$CAT_MANUAL = $this->category_manual_tournament_entity->init('id', $_POST['cat_id']);
		$this->bet_manual_tournament_entity->processApplyAnswer($CAT_MANUAL->getKeyId());
		
		if($this->bet_manual_tournament_entity->isBettingStatusCategory($CAT_MANUAL->getKeyId())){
			echo "Please set result before.";exit();
		}
		$bettingList = $this->bet_manual_tournament_entity->getBettingList($CAT_MANUAL->getKeyId());
		foreach($bettingList as $item){
			$BET_MANUAL = $this->bet_manual_tournament_entity->init('id',$item->id);
			$BET_MANUAL->setStatus('finished');
			$BET_MANUAL->processUserBetResult();
			$BET_MANUAL->update();
		}
		$CAT_MANUAL->setStatus('closed');
		$CAT_MANUAL->update();
		
		echo $CAT_MANUAL->getTournamentId();exit();
	} 
	
	function apply_answer_to_category(){
		$cat_id = $this->input->post('cat_id', TRUE);
		$CAT = $this->category_manual_tournament_entity->init('id', $cat_id);
		if($CAT->getFixAnswer() != 1){
			echo "Please set correct answer and fix answer before.";exit();
		}
		$this->bet_manual_tournament_entity->processApplyAnswer($CAT->getKeyId());
		echo $cat_id;exit();
	}
	
	function apply_answer_to_all_category_of_match(){
		$tournament_id = $this->input->post('tournament_id', TRUE);
		$cateArray = $this->category_manual_tournament_entity->getLivingCategoriesOfMatch($tournament_id);
		
		$canApply = true;
		foreach($cateArray as $item){
			if($item->fix_answer != 1){
				$canApply = false;
				break;
			}
		}
		
		if(!$canApply){
			echo "Please set correct answer and fix answer of all categories before.";exit();
		}
		
		foreach($cateArray as $item){
			$this->bet_manual_tournament_entity->processApplyAnswer($item->key_id);
		}
		echo "Apply answer done.";
		exit();
	}
	
	
	
	
	
	
//end class
}	